package com.af.robot.util;

import java.math.BigDecimal;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

public class Unit {

    public static String millis2TimeString(long millis){
        DateTimeFormatter ftf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return ftf.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(millis), ZoneId.systemDefault()));
    }

    public static String byte2SizeString(long file){
        char[] symbols =new char[]{'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'};
        int index = -1;
        double bytes = file;
        while (bytes > 1024){
            index ++ ;
            bytes = bytes / 1024.0;
        }
        return index == -1? String.format("%.2f",bytes)+"B" : String.format("%.2f",bytes) + symbols[index] + "B";
    }
}
